<script setup lang="ts">
import { useLoadMore } from 'vue-request'
import { useUserInfoStore } from '@/store/userInfo'
import { fetchDistributionDetailAPI } from '@/apis/fetchDistributionDetail'

const userInfoStore = useUserInfoStore()

const id = ref('')

// 分页列表数据
const { dataList, loading, loadingMore, noMore, loadMore, refresh } = useLoadMore<LoadMoreData<DistributionItem>>(
  fetchData,
  { isNoMore: d => d?.list.length === d?.total, manual: true },
)

// “加载更多”的状态
const loadMoreStatus = computed(() => {
  if (loadingMore.value)
    return 'loading'
  else if (noMore.value)
    return 'nomore'
  else
    return 'loadmore'
})

onLoad((options) => {
  if (!options?.id)
    return
  id.value = options.id
  refresh()
})
onReachBottom(() => loadMore())

/**
 * @description 分页服务函数
 */
async function fetchData(d?: LoadMoreData<DistributionItem>): Promise<LoadMoreData<DistributionItem>> {
  // 当前页数
  const _page = d?.page ? d.page + 1 : 1

  const res = await fetchDistributionDetailAPI(
    { id: id.value, page: _page.toString(), limit: '10' },
    userInfoStore.userInfo!.token!,
  )

  return {
    list: res.data?.data.data || [],
    page: _page,
    total: res.data?.data.total || 9999,
  }
}
</script>

<template>
  <Spacer height="40" />
  <view v-for="item in dataList" :key="item.title" class="mb-24rpx rounded-20rpx bg-white p-24rpx">
    <view class="flex items-center justify-between">
      <view class="text-32rpx text-#333333 font-bold leading-45rpx">
        {{ item.title }}
      </view>
      <view class="text-36rpx text-#1B85FF font-medium leading-50rpx">
        {{ item.money }}
      </view>
    </view>
    <Spacer height="24" />
    <Divider width="654" />
    <Spacer height="24" />
    <view class="flex items-center">
      <view class="min-w-150rpx text-28rpx text-#333333 font-medium leading-40rpx">
        邀请时间：
      </view>
      <view class="text-28rpx text-#848484 leading-40rpx">
        {{ item.createtime }}
      </view>
    </view>
    <Spacer height="12" />
    <view class="flex items-center">
      <view class="min-w-150rpx text-28rpx text-#333333 font-medium leading-40rpx">
        进店时间：
      </view>
      <view class="text-28rpx text-#848484 leading-40rpx">
        {{ item.create_time }}
      </view>
    </view>
  </view>
  <uv-load-more v-if="dataList.length > 3" :status="loadMoreStatus" @loadmore="loadMore" />
  <Spacer height="200rpx" />
  <uv-loading-page :loading="loading && !loadingMore" loading-text="加载中..." font-size="28rpx" loading-mode="spinner" />
</template>

<route lang="yaml">
name: 'distributionDetail'
style:
  navigationBarTitleText: '分销明细'
</route>
